<template>
  <div class="">
    <tic-tok
      ref="tictok"
      :t0="t0"
      :t1="t1"
      :t2="t2"
      :t3="t3"
      :t4="t4"
      :base-time="baseTime"
      :wait-times="waitTimes"
    />
  </div>
</template>
<script>
/**
 * 控制器
 */
import ticTok from "./tictok";
export default {
  name: 'Controller',
  components: { ticTok },
  props: {
    waitTimes: {
      type: Number,
      default() {
        return 1000;
      }
    },
    baseTime: {
      type: Number,
      default() {
        return 1000;
      }
    },
    catchValue: {
      type: Object,
      default() {
        return {
          order: [],
          status: -1
        };
      }
    },
    orderStatus: {
      type: String,
      default() {
        return "q0";
      }
    },
    readPosition: {
      type: Number,
      default() {
        return 0;
      }
    },
    orders: {
      type: Object,
      default() {
        return {};
      }
    },
    power: {
      type: Boolean,
      default() {
        return false;
      }
    },
    memery: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
    };
  },
  watch: {
    power(newData) {
      if (newData) this.$refs.tictok.init();
      else this.$refs.tictok.flag = false;
    }
  },
  mounted() {
    // 
  },
  methods: {
    setDate(key, value) {
      this.$emit("changeData", {key, value});
    },
    stop() {
      this.setDate("power", false);
    },
    start() {
      this.setDate("power", true);
    },
    writeCode({position = 0, data = -2}) {
      if (data === -2 || position < 0 || position > this.memery.length) return;
      this.$emit("changeMemery", {position, data});
    },
    t0() {
      this.$emit("changeProgramTime", 0);
      console.log(`读取数据... catch ${this.memery[this.readPosition]}`);
      if (this.memery[this.readPosition] === 0) {
        this.setDate("catchValue", {
          ...this.catchValue,
          status: 0
        });
      } else if (this.memery[this.readPosition] === 1) {
        this.setDate("catchValue", {
          ...this.catchValue,
          status: 1
        });
      } else {
        this.setDate("catchValue", {
          ...this.catchValue,
          status: -1
        });
      }
    },
    t1() {
      this.$emit("changeProgramTime", 1);
      // 
      console.log(`匹配规则... catch ${this.orders[this.orderStatus][this.catchValue.status]}`);
      this.setDate("catchValue", {
        ...this.catchValue,
        order: this.orders[this.orderStatus][this.catchValue.status]
      });
    },
    t2() {
      this.$emit("changeProgramTime", 2);
      // 
      console.log(`写入数据... write ${this.catchValue.order[2]}`);
      this.writeCode({position: this.readPosition, data: this.catchValue.order[2]});
    },
    t3() {
      this.$emit("changeProgramTime", 3);
      // 
      console.log(`切换位置... to ${this.catchValue.order[3]}`);
      if (this.catchValue.order[3] !== "H") {
        let num = this.readPosition + this.catchValue.order[3];
        this.setDate("readPosition", num);
      } else {
        console.log("停机成功");
        this.stop();
      }
    },
    t4() {
      this.$emit("changeProgramTime", 4);
      // 
      console.log(`切换状态并等待下一周期...turn ${this.catchValue.order[4]}`);
      this.setDate("orderStatus", this.catchValue.order[4] || 0);
    },
  }
};
</script>